package com.xsherl.leetcode.solution;

import com.xsherl.leetcode.base.TreeNode;
import com.xsherl.leetcode.utils.PrintUtils;

import java.util.ArrayList;
import java.util.List;

public class BinaryTreeLevelOrderTraversal {

    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        levelOrder(root, 0, res);
        return res;
    }

    public void levelOrder(TreeNode root, int level, List<List<Integer>> res){
        if (root == null){
            return;
        }
        while (res.size() <= level){
            res.add(new ArrayList<>());
        }
        res.get(level).add(root.val);
        levelOrder(root.left, level + 1, res);
        levelOrder(root.right, level + 1, res);
    }

    public static void main(String[] args) {
        TreeNode root = TreeNode.buildTree(3,9,20,null,null,15,7);
        List<List<Integer>> lists = new BinaryTreeLevelOrderTraversal().levelOrder(root);
        PrintUtils.println(lists);
    }

}
